package com.webbei.HAO.action;

import org.apache.struts2.ServletActionContext;
import org.json.JSONException;
import org.json.JSONObject;

import cc.denglu.sdk.Denglu;
import cc.denglu.sdk.DengluException;

import com.webbei.HAO.service.ChSer;
import com.webbei.pojo.DengLu;
import com.webbei.pojo.ThirdPartyInfo;
import com.webbei.product.service.ProductService;
/**
 * 登录回调
 * @author xingxing
 *
 */
public class TokenAction {
	private String token;
	private ProductService ps;
	private ChSer ser;
	private String msg;
	private DengLu d;
	private String name;
	private ThirdPartyInfo thirdPartyInfo;
	private String html;
	private String redirect_uri;
	public String execute(){
		String str=null;
		// Map<String,String> m= ps.getDengLuKey("register");
		String param = ServletActionContext.getRequest().getQueryString();
		System.out.println("param:"+param);
		d = ps.getDengLuByName(name);
		 String app_id = d.getApp_id();
		 String api_key =d.getApi_key();
		 Denglu denglu;
		try {
			denglu = new Denglu(app_id, api_key, null, null);
			JSONObject jsonObj = denglu.getUserInfoByToken(token);
			msg = jsonObj.toString();
			Object openid = jsonObj.get("mediaUserID");
			Object thirdTypeId = jsonObj.get("mediaID");
			System.out.println("thirdTypeId:"+thirdTypeId);
			
			thirdPartyInfo = ser.getThirdPartyById(thirdTypeId);
			System.out.println("third:"+thirdPartyInfo);
			Object screenName = jsonObj.get("screenName");
			String username = ser.customercheck3(openid.toString());
			if("add".equals(name)){
				ServletActionContext.getRequest().getSession().setAttribute("app_name",name);
				html = "<font color=red>"+screenName
	               +"</font><img src=thirdparty/"+thirdPartyInfo.getImg()
	               +"  />"+"<input type=button value=添加  onclick=addThird("+thirdTypeId+") />";
				ServletActionContext.getRequest().getSession().setAttribute("screenName",screenName);
				ServletActionContext.getRequest().getSession().setAttribute("imgSrc","/thirdparty/"+thirdPartyInfo.getImg());
				ServletActionContext.getRequest().getSession().setAttribute("thirdTypeId",thirdTypeId);
				ServletActionContext.getRequest().getSession().setAttribute("openid",openid.toString());
				str = "add";
			}
			 if(username!=null&&"login".equals(name)){
				ServletActionContext.getRequest().getSession().setAttribute("username",username);
				str = "login";
			}
			 if(username!=null&&"register".equals(name)){
				ServletActionContext.getRequest().getSession().setAttribute("username",username);
				str = "login";
			 }
			 if(username==null&&!"add".equals(name)){
				System.out.println("register.....");
				ServletActionContext.getRequest().getSession().setAttribute("openid",openid.toString());
				ServletActionContext.getRequest().getSession().setAttribute("thirdTypeId",thirdTypeId.toString());
				ServletActionContext.getRequest().getSession().setAttribute("screenName",screenName.toString());
				str = "register";
			}
			 System.out.println("return:"+str);
		} catch (DengluException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		} 
		return str;
	}
	public String getToken() {
		return token;
	}
	public void setToken(String token) {
		this.token = token;
	}

	public void setPs(ProductService ps) {
		this.ps = ps;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public void setSer(ChSer ser) {
		this.ser = ser;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public ThirdPartyInfo getThirdPartyInfo() {
		return thirdPartyInfo;
	}
	public void setThirdPartyInfo(ThirdPartyInfo thirdPartyInfo) {
		this.thirdPartyInfo = thirdPartyInfo;
	}
	public String getRedirect_uri() {
		return redirect_uri;
	}
	public void setRedirect_uri(String redirectUri) {
		redirect_uri = redirectUri;
	}
}
